<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    :paddBottom="5"
  >
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="部门名称:" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="deptTree"
      v-if="refreshTable"
      border
      height="730"
      v-loading="loading"
      :data="deptList"
      :row-key="
        (row) => {
          return row.deptId;
        }
      "
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      :default-expand-all="isExpandAll"
    >
      <!-- :expand-row-keys="expandsArr" -->
      <!-- @expand-change="handleExpandSelect" -->
      <el-table-column
        prop="deptName"
        label="部门名称"
        min-width="200"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="orderNum"
        align="center"
        label="排序"
        width="80"
      ></el-table-column>
      <el-table-column prop="type" align="center" label="部门类型" width="120">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_dept_type"
            :value="scope.row.type"
          />
        </template>
      </el-table-column>
      <el-table-column prop="manageDeptname" align="center" label="主管单位">
      </el-table-column>
      <el-table-column prop="status" align="center" label="状态" width="100">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_normal_disable"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" fixed="right" width="480">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-plus"
            @click="showModel('operate', scope.row)"
            v-hasPermi="['system:dept:add']"
          >
            新增机构
          </el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="showModel('operate3', scope.row)"
            v-hasPermi="['system:dept:edit']"
          >
            修改名称
          </el-button>
          <el-button
            v-if="scope.row.status == 0"
            size="small"
            type="text"
            icon="el-icon-bottom"
            @click="handlerCheck('停用', scope.row)"
            style="color: #f56c6c"
            v-hasPermi="['system:dept:edit']"
          >
            停用
          </el-button>
          <el-button
            v-if="scope.row.status == 1"
            size="small"
            type="text"
            icon="el-icon-top"
            @click="handlerCheck('启用', scope.row)"
            v-hasPermi="['system:dept:edit']"
          >
            启用
          </el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="handlerCheck('删除', scope.row)"
            style="color: #f56c6c"
            v-hasPermi="['system:dept:remove']"
          >
            删除
          </el-button>
          <el-button
            v-if="scope.row.type != 4"
            size="small"
            type="text"
            icon="el-icon-s-tools"
            @click="showModel('operate2', scope.row)"
            v-hasPermi="['system:dept:edit']"
          >
            主管单位
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <component
      :is="modelName"
      :id="modelid"
      :manageDeptId="manageDeptId"
      :chosedeptName="chosedeptName"
      :visible.sync="show"
      @close="close"
    ></component>
  </lb-layout>
</template>

<script>
import {
  getDepttree,
  listDept,
  getDeptdisable,
  getDeptenable,
  delDept,
} from "@/api/system/orgdept";
import operate from "@/views/configuration/organization/dept/components/operate.vue";
import operate2 from "@/views/configuration/organization/dept/components/operate2.vue";
import operate3 from "@/views/configuration/organization/dept/components/operate3.vue";
export default {
  components: {
    operate,
    operate2,
    operate3,
  },
  dicts: ["sys_normal_disable", "sys_dept_type"],
  data() {
    return {
      isExpandAll: true,
      // 显示搜索条件
      showSearch: true,
      //弹窗
      modelid: "",
      modelName: "",
      manageDeptId: null,
      chosedeptName: "",
      show: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 遮罩层
      loading: true,
      // 表格树数据
      deptList: [],
      // 展开的部门的ID集合
      expandsArr: [],
      // 查询参数
      queryParams: {
        deptName: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {},
  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      listDept(this.queryParams)
        .then((res) => {
          console.log(res, "deptlist");
          this.loading = false;
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let arr = res?.data ?? [];
          if (arr.length > 0) {
            arr.map((item) => {
              if (item.manageDeptId != null) {
                arr.map((tig) => {
                  if (item.manageDeptId == tig.deptId) {
                    item.manageDeptname = tig.deptName;
                  } else {
                    // item.manageDeptname = "暂无主管部门";
                  }
                });
              } else {
                // item.manageDeptname = "暂无主管部门";
              }
            });
          }
          this.deptList = this.handleTree(arr, "deptId");
          console.log(this.deptList, "deptList");
          // this.defaultExpandedfirst();
        })
        .catch(() => {
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    /** 默认展开表格树结构第一层 */
    defaultExpandedfirst() {
      this.$nextTick(() => {
        this.$refs.deptTree.toggleRowExpansion(this.deptList[0], true);
      });
      this.expandsArr.push(this.deptList[0].deptId);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 弹窗
    showModel(name, params) {
      this.modelName = name;
      this.modelid = params?.deptId || "";
      this.manageDeptId = params?.manageDeptId ?? null;
      this.chosedeptName = params?.deptName ?? "";
      setTimeout(() => {
        this.show = true;
      }, 200);
    },
    close(status) {
      this.show = false;
      setTimeout(() => {
        if (status) this.getList();
      }, 200);
    },
    // 确认框
    handlerCheck(type, row) {
      const text = "是否确定" + type + (row?.deptName ?? "该条数据") + "？";
      let queryparams;
      let requestWay;
      switch (type) {
        case "停用":
          requestWay = getDeptdisable;
          queryparams = {};
          queryparams.deptId = row.deptId;
          break;
        case "启用":
          requestWay = getDeptenable;
          queryparams = {};
          queryparams.deptId = row.deptId;
          break;
        case "删除":
          requestWay = delDept;
          queryparams = row.deptId;
          break;
      }
      this.$modal
        .confirm(text)
        .then(() => {
          requestWay(queryparams)
            .then((res) => {
              if (res.code === 700) {
                this.$message.warning(res.msg);
                return false;
              }
              this.$message.success("已" + type + "!");
              this.getList();
            })
            .catch((err) => {
              // this.$message.error("系统错误,请联系管理员!");
            });
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
